#pragma once 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <mutex>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"


namespace ns_util
{
    class FileUtil
    {   
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path,std::ios::in);
           if(!in.is_open())//c++中文件处理函数
           {
                std::cerr <<"open in error2"<<std::endl;
                return false;
           }
            std::string line;
            while(std::getline(in,line))//将文件内容写入到（string）out中
            {
                *out += line;
                //std::cout<<line<<std::endl;
            }
            in.close();//关闭文件！！！
            return true;
        }
    };

    class StringUtil//切割字符串
    {
    public:
        static void Split(const std::string &target, std::vector<std::string> *out, const std::string &sep)
        {
            //boost split
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    const char* const DICT_PATH = "/home/SSS/cppjieba/dict/jieba.dict.utf8";
    const char* const HMM_PATH = "/home/SSS/cppjieba/dict/hmm_model.utf8";
    const char* const USER_DICT_PATH = "/home/SSS/cppjieba/dict/user.dict.utf8";
    const char* const IDF_PATH = "/home/SSS/cppjieba/dict/idf.utf8";
    const char* const STOP_WORD_PATH = "/home/SSS/cppjieba/dict/stop_words.utf8";
    class JiebaUtil
    {
    private:
        static cppjieba::Jieba _jieba;//不去暂停词
    private:
        //去暂停词
        //cppjieba::Jieba _jieba;//创建对象
        //std::unordered_map<std::string, bool> _stop_words;//哈希映射
        //static JiebaUtil* _instance;
    private:
        //JiebaUtil():_jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH) {}
        //JiebaUtil(const JiebaUtil &) = delete;
        //ns_util::JiebaUtil operaror=(const JiebaUtil& x)=delete;
    public:
        //去暂停词
        // static JiebaUtil* GetInstance()//初始化
        // {
        //     static std::mutex _mtx;
        //     if(nullptr==_instance)
        //     {
        //         _mtx.lock();
        //         if(nullptr==_instance)
        //         {
        //             _instance=new JiebaUtil();
        //             _instance->InitJiebaUtil();//加载文件
        //         }
        //         _mtx.unlock();
        //     }
        //     return _instance;
        //     //return nullptr;
        // }
        // void InitJiebaUtil()
        // {
        //     std::ifstream in(STOP_WORD_PATH);
        //     if(!in.is_open()) 
        //     {
        //         std::cerr<<"open file error"<<std::endl;
        //         return;
        //     }
        //     std::string line;
        //     while(std::getline(in,line))
        //     {
        //         _stop_words.insert({line, true});
        //     }
        //     in.close();
        // }
        // void CutStringHelper(const std::string &src, std::vector<std::string> *out)//去暂停词
        // {
        //     _jieba.CutForSearch(src, *out);//切分
        //     for(auto iter = out->begin(); iter != out->end();)
        //     {
        //         auto it=_stop_words.find(*iter);
        //         if(it!=_stop_words.end())
        //         {
        //             iter = out->erase(iter);//去暂停词
        //         }
        //         else iter++;
        //     }
        // }
    public:
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            //去暂停词，如果云服务器配置高的情况下可以使用，性能不高可能导致出现不必要的错误
            //ns_util::JiebaUtil::GetInstance()->CutStringHelper(src, out);
            //低配版，不去暂停词
           _jieba.CutForSearch(src, *out);
        }
        
    };
    //不去暂停词
    cppjieba::Jieba JiebaUtil::_jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
    //去暂停词
    //JiebaUtil* _instance=nullptr;
}
